package com.cn.config;

import com.cn.entity.Goods;
import com.cn.entity.SeckillGoods;
import com.cn.service.GoodsService;
import com.github.benmanes.caffeine.cache.CacheWriter;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.RemovalCause;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author: chenyuncong
 * @Date: 2020/6/28 14:51
 */
@Configuration
@Slf4j
public class CacheConfig {
    @Autowired
    private GoodsService goodsService;

    @Bean
    public LoadingCache<Long, Goods> getGoodsCache() {
        LoadingCache<Long, Goods> loadingCache = Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(1000)
                //缓存写入/删除监控
                .writer(new CacheWriter<Object, Object>() {
                    @Override
                    public void write(Object key, Object value) { //此方法是同步阻塞的
                        log.info("--缓存写入--:key=[{}], value=[{}]",key, value);
                    }
                    @Override
                    public void delete(Object key, Object value, RemovalCause cause) {
                        log.info("--缓存删除--:key=[{}]", key);
                    }
                })
                .expireAfterAccess(10*60, TimeUnit.MINUTES) //单位秒，过期时间 10分钟
                .build((Long key) -> {
                    log.info("查询id：[{}]",key);
                    Goods g = goodsService.getById(key);
                    if(g==null){
                        return null;
                    }
                    return g;
                }); //cacheload实现类,刷新时候调用

        return loadingCache;
    }

//    SeckillGoods
    @Bean
    public LoadingCache<String, SeckillGoods> getSeckillCache() {
        LoadingCache<String, SeckillGoods> loadingCache = Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(1000)
                //缓存写入/删除监控
                .writer(new CacheWriter<Object, Object>() {
                    @Override
                    public void write(Object key, Object value) { //此方法是同步阻塞的
                        log.info("--缓存写入--:key=[{}], value=[{}]",key, value);
                    }
                    @Override
                    public void delete(Object key, Object value, RemovalCause cause) {
                        log.info("--缓存删除--:key=[{}]", key);
                    }
                })
                .expireAfterAccess(10*60, TimeUnit.MINUTES) //单位秒，过期时间 10分钟
                .build((String key) -> {
//                    log.info("查询id：[{}]",key);
//                    Goods g = goodsService.getById(key);
//                    if(g==null){
//                        return null;
//                    }
                    return null;
                }); //cacheload实现类,刷新时候调用

        return loadingCache;
    }

    @Bean
    public LoadingCache expiryCache() {
        LoadingCache<String, String> loadingCache = Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(1000)
                //缓存写入/删除监控
                .writer(new CacheWriter<Object, Object>() {
                    @Override
                    public void write(Object key, Object value) { //此方法是同步阻塞的
                        System.out.println("--缓存写入--:key=" + key + ", value=" + value);
                    }

                    @Override
                    public void delete(Object key, Object value, RemovalCause cause) {
                        System.out.println("--缓存删除--:key=" + key);
                    }
                })
                .expireAfterAccess(10*60, TimeUnit.MINUTES) //单位秒，过期时间 10分钟
                .build((String key) -> {
                    System.out.println(key);
                    if(key.equals("aa")){
                        return "";
                    }
                    return null;
                }); //cacheload实现类,刷新时候调用

//        loadingCache.put("name","侯征");
        return loadingCache;
    }
}
